﻿using System;
using System.IO;
using System.Windows.Forms;

namespace homework5e3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Load += Form1_Load; // 将 Form1_Load 事件处理程序与 Load 事件关联
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            MergeFiles();
        }

        private void MergeFiles()
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.Multiselect = false;

            OpenFileDialog openFileDialog2 = new OpenFileDialog();
            openFileDialog2.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog2.Multiselect = false;

            if (openFileDialog1.ShowDialog() == DialogResult.OK && openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                string file1Path = openFileDialog1.FileName;
                string file2Path = openFileDialog2.FileName;

                string dataDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data");
                Directory.CreateDirectory(dataDirectory);
                string mergedFilePath = Path.Combine(dataDirectory, "read.txt");

                try
                {
                    using (StreamWriter writer = new StreamWriter(mergedFilePath))
                    {
                        string file1Contents = File.ReadAllText(file1Path);
                        string file2Contents = File.ReadAllText(file2Path);

                        writer.WriteLine(file1Contents);
                        writer.WriteLine(file2Contents);
                    }

                    MessageBox.Show("Files merged successfully.");
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"An error occurred: {ex.Message}");
                }
            }
            else
            {
                MessageBox.Show("Please select both files.");
            }
        }
    }
}
